-
-
Notifications
You must be signed in to change notification settings - Fork 974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #2531 value type on function overload #2561
fixes #2531 value type on function overload #2561
Conversation
🦋 Changeset detectedLatest commit: 3001b93 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@Yuripetusko is attempting to deploy a commit to the Wevm Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me. Would be good to get @tmm eyes on it too!
src/types/contract.ts
Outdated
@@ -303,10 +303,19 @@ export type EventDefinition = `${string}(${string})` | |||
|
|||
export type GetValue< | |||
abi extends Abi | readonly unknown[], | |||
functionName extends string, | |||
mutability extends AbiStateMutability = AbiStateMutability, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally mutability should be inferred also, but it's hard to do because of the order of generics arguments (args would be needed to infer mutability on overloads, and mutability is needed to infer args). Also it sucks that it's a second argument of this Generic (because subsequent arguments require it), as it makes it hard to skip it if you don't want to pass it. But I couldn't think of a better way right now, or it would require a bigger refactor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since GetValue
is a public API (exported in index.ts
), let's pull this logic directly into simulateContract.ts
for now.
test/src/abis.ts
Outdated
@@ -1363,6 +1363,16 @@ export const wagmiContractConfig = { | |||
stateMutability: 'nonpayable', | |||
type: 'function', | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this config has a contract address, do I need to add this function to the actual contract? or it's fine to just have this dummy/placeholder abi for a contract that is not actually deployed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this addition to the ABI.
Will take a look tomorrow! |
bump |
…om/Yuripetusko/viem into fix/2532-value-type-overload-types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's revert the change to GetValue
and update SimulateContractParameters
directly with this logic. Also, let's add the following test. (Haven't found a quick way to get @ts-expect-error
to work correctly in this case so might be best to remove that for now. There's a larger type refactor coming to Viem, which will fix this so allowing it to pass for the time being isn't the end of the world.)
test('https://github.com/wevm/viem/issues/2531', async () => {
const abi = parseAbi([
'function safeTransferFrom(address, address, uint256)',
'function safeTransferFrom(address, address, uint256, bytes) payable',
])
const res1 = await simulateContract(client, {
address: '0x',
abi,
functionName: 'safeTransferFrom',
args: ['0x', '0x', 123n],
// @ts-expect-error
value: 123n,
})
assertType<void>(res1.result)
expectTypeOf(res1.request.abi).toEqualTypeOf(
parseAbi(['function safeTransferFrom(address, address, uint256)']),
)
const res2 = await simulateContract(client, {
address: '0x',
abi,
functionName: 'safeTransferFrom',
args: ['0x', '0x', 123n, '0x'],
value: 123n,
})
assertType<void>(res2.result)
expectTypeOf(res2.request.abi).toEqualTypeOf(
parseAbi([
'function safeTransferFrom(address, address, uint256, bytes) payable',
]),
)
})
In summary:
- Remove
GetValue
related changes - Add logic directly to
SimulateContractParameters
- Add test to
simulateContract.test-d.ts
src/types/contract.ts
Outdated
@@ -303,10 +303,19 @@ export type EventDefinition = `${string}(${string})` | |||
|
|||
export type GetValue< | |||
abi extends Abi | readonly unknown[], | |||
functionName extends string, | |||
mutability extends AbiStateMutability = AbiStateMutability, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since GetValue
is a public API (exported in index.ts
), let's pull this logic directly into simulateContract.ts
for now.
test/src/abis.ts
Outdated
@@ -1363,6 +1363,16 @@ export const wagmiContractConfig = { | |||
stateMutability: 'nonpayable', | |||
type: 'function', | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this addition to the ABI.
src/types/contract.test-d.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's revert this change.
@Yuripetusko nice find. Can drop |
Weirdly I can't reproduce this in here. So maybe it's my Jetbrains IDE messing with me again https://stackblitz.com/edit/viem-getting-started-jixksa?file=package.json,index.ts |
@tmm added it to writeContract too, but lmk if I should just add it to simulateContract. Unfortunately I keep getting http errors when running all tests locally. But selected tests seems to be fine. Ignore the previous comments about parseAbi, it was due to something else entirely (missing |
fixes #2531 - infer correct type of
value
on function overload, by matching correct function signature from a union type, given the function argsPR-Codex overview
This PR focuses on improving type inference for function values in contracts.
Detailed summary
GetMutabilityAwareValue
andSimulateContractParameters
insimulateContract.ts
simulateContract.ts
andwriteContract.ts
simulateContract.test-d.ts